package com.telnetnz.ticketdesk.action.user;

import org.apache.log4j.Logger;

import com.rockbb.common.sso.SessionService;
import com.telnetnz.ticketdesk.action.base.BasicSessionPageAction;
import com.telnetnz.ticketdesk.bean.User;
import com.telnetnz.ticketdesk.service.UserService;

public class AnonymousAction extends BasicSessionPageAction
{
	private static Logger logger = Logger.getLogger(AnonymousAction.class);

	public String doLogin()
	{
		LoginBean bean = new LoginBean();

		if (isLogged()) 
		{
			return this.message_box(
					langRes.get("global.system-message"), 
					langRes.get("user.already-logged-message", new String[]{bean.getRedirect()}),
					bean.getRedirect(),
					2);
		}

		if (bean.isSubmitted() && bean.validate()) 
		{
			User user = UserService.getByName(bean.getUsername());
			SessionService.login(
					getResponse(), 
					getSession().getSession_id(), 
					user.getId(),
					bean.getAutologin());

			// Reload session and user without touch cookies, the cookies have been set in above login method
			reloadSession(getSession().getSession_id());

			return this.message_box(
					langRes.get("user.login-success"), 
					langRes.get("user.login-success-message", new String[]{bean.getRedirect()}),
					bean.getRedirect(),
					2);
		}
		this.put_page_key("bean", bean);
		this.put_page_key("post_action", baseLink());
		this.put_page_key("retrieve_password_action", baseLink("userpasswordretrieve"));

		return INPUT;
	}

	public String doLogout()
	{
		DefaultRequestBean bean = new DefaultRequestBean();

		if (!isLogged()) 
		{
			return this.message_box(
					langRes.get("user.need-register-or-login"), 
					langRes.get("user.need-register-or-login-message", new String[]{baseLink("userlogin")}),
					baseLink("userlogin"),
					2);
		}

		SessionService.logout(getResponse(), getSession().getSession_id());
		return this.message_box(
				langRes.get("user.logout-success"), 
				langRes.get("user.logout-success-message", new String[]{bean.getRedirect()}),
				bean.getRedirect(),
				2);
	}

	public class LoginBean extends RequestBean
	{
		private String username;
		private String password;
		private int autologin;

		public final String getUsername()
		{
			return username;
		}
		public final String getPassword()
		{
			return password;
		}
		public final int getAutologin()
		{
			return autologin;
		}

		@Override
		protected void loadRequest()
		{
			username = get("username");
			password = get("password");
			autologin = getInt("autologin");
		}

		@Override
		public boolean validate()
		{
			boolean flag = true;
			if (this.getUsername().trim().getBytes().length < 3)
			{
				flag = false;
				this.put_error_msg("username", langRes.get("user.content-cannot-be-empty"));
			}
			if (this.getPassword().length() == 0)
			{
				flag = false;
				this.put_error_msg("password", langRes.get("user.content-cannot-be-empty"));
			}
			else if (!UserService.checkAuthorization(this.username, this.password))
			{
				flag = false;
				this.put_error_msg("password", langRes.get("user.password-incorrect"));
			}
			return flag;
		}
	}

}
